package com.kgc.mall.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    //处理修改用户密码请求的方法
    public void updatePassword(
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("修改用户密码");
    }

    //处理修改用户信息请求的方法
    public void modifyUser(
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("修改用户信息");
    }

    //处理用户登录请求的方法
    protected void login(
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession userSession = request.getSession();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if ("admin".equals(username) && "123456".equals(password)) {
            userSession.setAttribute("curName","admin");
            response.sendRedirect("/index.jsp"); //重定向
        } else {
            request.setAttribute("retMsg","用户名或者密码不正确");
            request.getRequestDispatcher("/login1.jsp").forward(request,response);//转发
        }
    }

    //处理用户注册请求的方法
    public void register(
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        String repwd = request.getParameter("repwd");
        String sex = request.getParameter("sex");
        String[] hobby = request.getParameterValues("hobby");
        String problem = request.getParameter("problem");
        String answer = request.getParameter("answer");
        String introduce = request.getParameter("introduce");
    }

    //处理用户退出系统请求的方法
    public void logout(
            HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession userSession = request.getSession();
        userSession.invalidate();
        response.sendRedirect("/login1.jsp");
    }
}
